/*
 * -------------------------------------------------------------------------------------------------------*
 * SBS Priceboard V2.0
 * Copyright (C) 2008 Unicom Vietnam JSC
 * -------------------------------------------------------------------------------------------------------* 
 * HeadURL: http://192.168.1.4/svn/priceboard/trunk/SBS_PriceboardV2/front-end/end-user/enduser_priceboard/
 * LastChangeBy: quanhm
 * LastChangeDate: 2008/04/19
 */
package com.unicom.priceboard.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import com.unicom.priceboard.thread.TimerThread;

/**
 * Creation date: Apr 16, 2008
 * @author quan
 */
public class Util {
	
	public static String getCurrentDataPath(String dataPath) {
		GregorianCalendar gCal = new GregorianCalendar();
		
		if(TimerThread.CURRENT_DAY_OF_WEEK==7) {
			gCal.add(GregorianCalendar.DAY_OF_YEAR, -1);
		}else if(TimerThread.CURRENT_DAY_OF_WEEK==1) {
			gCal.add(GregorianCalendar.DAY_OF_YEAR, -2);
		}
		
		int curDay = gCal.get(GregorianCalendar.DATE);
		
		String dayPattern = curDay<10?"0"+curDay:String.valueOf(curDay);
		//System.out.println(dayPattern);
		String ret=dataPath.replaceAll("BACKUPXX", "BACKUP"+dayPattern);
		//System.out.println("testing datapath: " + ret);
		return ret;
	}
	
	public static boolean checkBackup(String dataPath){
		boolean backup=false;
		if(dataPath.contains("BACKUP")||dataPath.contains("backup"))
			backup=true;
		
		return backup;
	}
	
	public static String getPreTime(String dataPath) {
		GregorianCalendar gCal = new GregorianCalendar();
		
		if(TimerThread.CURRENT_DAY_OF_WEEK==7) {
			gCal.add(GregorianCalendar.DAY_OF_YEAR, -1);
		}else if(TimerThread.CURRENT_DAY_OF_WEEK==1) {
			gCal.add(GregorianCalendar.DAY_OF_YEAR, -2);
		}
		if(TimerThread.CURRENT_DAY_OF_WEEK==2)
			gCal.add(GregorianCalendar.DAY_OF_YEAR, -2);
		int curDay = gCal.get(GregorianCalendar.DATE)-1;
		
		String dayPattern = curDay<10?"0"+curDay:String.valueOf(curDay);
		String ret=dataPath.replaceAll("BACKUPXX", "BACKUP"+dayPattern);
		return ret;
	}
	
	public static String getCurrentDate(String pattern) {
		DateFormat df = new SimpleDateFormat(pattern);
		return df.format(new Date());
	}
}
